var app = getApp()
let db = wx.cloud.database();
Page({
  data: {
    // 顶部菜单切换
    navbar: ["我发布的", "待发货", "待用户评价", "已完成"],
    // 默认选中菜单
    currentTab: 0,
    goodList: [],
    orderList: [],
  },
  onShow() {
    this.getMySellGood()
  },
  //顶部tab切换
  navbarTap: function (e) {
    let index = e.currentTarget.dataset.idx;
    this.setData({
      currentTab: index
    })
    if (index == 0) { //我发布的
      this.getMySellGood()
    } else if (index == 1) { //待发货
      //-1订单取消,0新下单发货,1已收货待评价,2订单已完成
      this.getOrderList(0);
    } else if (index == 2) { //待用户评价
      this.getOrderList(1)
    } else if (index == 3) { //已完成
      this.getOrderList(2)
    }
  },
  //我发布的商品
  getMySellGood() {
    let user = wx.getStorageSync('user')
    console.log('user.phone', user.phone)

    wx.request({
      url: 'http://localhost:8080/api/goods/all',
      method: 'POST',
      data: {
        action: 'seller',
        userid: user.id
      },
      success: (res) => {
        if (res.data.code == 200) {
          console.log('获取我发布的商品成功', res)
          this.setData({
            goodList: res.data.data
          })
        } else {
          console.log('获取我发布的商品失败', res)
        }
      }
    })
  },


  // 获取用户订单列表
  getOrderList(status) {
wx.request({
  url: 'http://localhost:8080/api/orders/list',
  method:'POST',
  data:{
    action: 'seller',
    status: status,
    sellerId: app.globalData.userInfo.id
  },
  success:(res)=>{
      if (res.data.code == 200) {
        console.log("用户订单列表",res.data.data)
        this.setData({
          list: res.data.data
        })
      }else{
        console.log("暂无数据")
      }
  }
})
},
  //去商品详情页
  goDetail(e) {
    wx.navigateTo({
      url: '/pages/detail/detail?goodid=' + e.currentTarget.dataset.id
    })
  },
  //删除商品
  delete(e) {
    let id = e.currentTarget.dataset.id
    wx.request({
      url: 'http://localhost:8080/api/goods/remove',
      method: 'POST',
      data: {
        action: 'remove',
        id: id
      },
      success: (res) => {
        if (res.data.code == 200) {
          console.log('删除成功', res)
          wx.showToast({
            title: '删除成功'
          })
          this.getMySellGood()
        } else {
          console.log('删除失败', res)
          wx.showToast({
            icon: 'none',
            title: '删除失败'
          })
        }
      }
    })




  },
  //去发布页
  goFabu() {
    wx.switchTab({
      url: '/pages/fabu/fabu',
    })
  },
  //已送货
  songda(e) {
    let id = e.currentTarget.dataset.id

    wx.request({
      url: 'http://localhost:8080/api/orders/action',
      method:'POST',
      data:{
        action: 'songda',
        id: id
      },
      success:(res)=>{
        if (res.data.code == 200) {
          console.log('修改订单成功', res)
      wx.showToast({
        title: '修改订单成功'
      })
      this.getOrderList(0)
        }else{
          console.log('修改订单失败', res)
          wx.showToast({
            icon: 'none',
            title: '修改订单失败'
          })
        }
      }
    })
  },
  // 给买家打电话
  call(e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.phone,
    })
  },
})